$(document).ready(function() {
    if ($.cookie("chatSendStyle")) {
        style = $.cookie("chatSendStyle");
        if (style == "ctrlEnter") {
            $("#chkCtrlEnter").attr("checked", "checked");
            $('#msg').removeAttr("onkeypress");
            $('#msg').attr("onkeypress", "ctrlEnterSendStyle(event);")            
        }
        if (style == "Enter") {
            $("#chkEnter").attr("checked", "checked");
            $('#msg').removeAttr("onkeypress");
            $('#msg').attr("onkeypress", "enterSendStyle(event);")             
        }    
    }
    
    $( "#btnSend" ).button({
        icons: {
            primary: "ui-icon ui-icon-triangle-1-e"
        },
        text: false
    })
    
    $( "#btnChatRefresh" ).button({
        icons: {
            primary: "ui-icon ui-icon-refresh"
        },
        text: false
    })

    $( "#btnLogout" ).button({
        icons: {
            primary: "ui-icon ui-icon-power"
        },
        text: false
    }) 
    
    $( "#btnUsrContentMin" ).button({
        icons: {
            primary: "ui-icon ui-icon-circle-triangle-n"
        },
        text: false
    }) 
    
    $( "#btnUsrContentMax" ).button({
        icons: {
            primary: "ui-icon ui-icon-circle-triangle-s"
        },
        text: false
    }) 
    

    $("#pass").keyup(function(event){
        if(event.keyCode == 13){
            $("#btnLoginSubmit").click();
        }
    });
});

function ctrlEnterSendStyle(event){
    text = $('#msg').val();
    if((event.ctrlKey) && ((event.keyCode == 0xA)||(event.keyCode == 0xD)))
    {
        $('#btnSend').click();
    }
}
function enterSendStyle(event){
    if((event.ctrlKey) && ((event.keyCode == 0xA)||(event.keyCode == 0xD)))
    {
        msg = $('#msg').val();
        msg += "\n";
        $('#msg').val(msg);
        return;
    }
    
    if(event.keyCode == 0xD)
    {
        $('#btnSend').click();
    }
}
function changeSendStyle(){
    style = $('input[name=sendStyle]:checked').val();
    if (style == "ctrlEnter") {
        $('#msg').removeAttr("onkeypress");
        $('#msg').attr("onkeypress", "ctrlEnterSendStyle(event);")
    }else{
        $('#msg').removeAttr("onkeypress");
        $('#msg').attr("onkeypress", "enterSendStyle(event);")        
    }
    
    $.cookie("chatSendStyle", style, {
        expires: 365
    });
    
    $('#msg').focus();
}

function showModalWindow(title, msg, window_width, window_height){
    $('#dialog-modal').attr("title", title);
    $('#dialog-modal').html(msg);        
    $("#dialog-modal" ).dialog({
        height: window_height,
        width: window_width,
        modal: true,
        buttons: [
        {
            text: "Закрыть окно",
            click: function() {
                $(this).dialog("close");
            }
        }
        ]
    });                    
}

function showModalWindowRedirect(title, msg, link,window_width, window_height){
    $('#dialog-modal').attr("title", title);
    $('#dialog-modal').html(msg);        
    $("#dialog-modal" ).dialog({
        height: window_height,
        width: window_width,
        modal: true,
        buttons: [
        {
            text: "Закрыть окно",
            click: function() {
                window.location = link; 
            }
        }
        ]
    });                    
}